package com.xbongbong.paas.script.controller;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.service.JsonSetService;
import com.xbongbong.sys.model.CompanyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program xbb-pro
 * @Description 用于刷产品相关的数据
 * @Author caoyy
 * @Date 2019/8/27
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/productNo")
public class ProductNoController {
    private static final Logger LOG = LoggerFactory.getLogger(ProductNoController.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private ProductModel productModel;
    @Resource
    private JsonSetService jsonSetService;
    @Resource
    private CompanyModel companyModel;

    /**
     * @Description: 用于将已有老数据的产品编号值刷到外面
     * @Param: [stockDTO, br]
     * @return: java.lang.String
     * @Author: cyy
     * @Date: 2019/8/27
     */
    @RequestMapping(value = "/productNo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initFormDic(@RequestBody @Valid ProductDTO productDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(productDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = productDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = companyModel.getCorpidList();
        }
        LOG.warn("数据库中共有公司" + corpidList.size() + "家");
        for (String corpid : corpidList) {
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put(ParameterConstant.CORPID, corpid);
            params.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
            Integer totalCount = productModel.getEntitysCount(params);
            LOG.warn("corpid:" + corpid + "数据库中共有产品" + totalCount + "条数据");
            int pageSize = 500;
            int totalPage = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize) + 1;
            int lastPageSize = (totalCount % pageSize == 0) ? pageSize : (totalCount % pageSize);
            for (int page = 0; page < totalPage; page++) {
                if (page == totalPage - 1) {
                    params.put("pageNum", lastPageSize);
                } else {
                    params.put("pageNum", pageSize);
                }
                params.put("start", page * pageSize);
                LOG.warn("第" + page + "页开始");
                List<ProductEntityExt> productEntityExtList = productModel.findEntitys(params);
                if (productEntityExtList.isEmpty()) {
                    continue;
                }
                List<UpdateDataEntity> updateDataEntityList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                productEntityExtList.forEach(item -> {
                    UpdateDataEntity updateDataEntity = new UpdateDataEntity();
                    updateDataEntity.setCorpid(corpid);
                    updateDataEntity.setId(item.getId());
                    updateDataEntity.setDel(DelEnum.NORMAL.getDel());
                    updateDataEntity.setSerialNo(item.getData().getString("text_2") == null ? "" : item.getData().getString("text_2"));
                    updateDataEntityList.add(updateDataEntity);
                });
                if (CollectionsUtil.isNotEmpty(updateDataEntityList)){
                    productModel.updateBatchSerialNo(updateDataEntityList);
                }
                LOG.warn("第" + page + "页结束");
            }
        }
        return null;
    }
}
